package com.xframe.xdal.core.util.word;


import com.xframe.xdal.core.util.XReflect;
import com.xframe.xdal.core.util.pdf.TableCol;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;

import java.math.BigInteger;
import java.util.Collection;

@Data
public class WordTableOpt {

    @Getter(AccessLevel.PUBLIC)
    @Setter(AccessLevel.PRIVATE)
    private WordBuilder wordBuilder;

    /**
     * 表格头部背景颜色
     */
    //private String tableHeadBackgroundColor = "4472C4";
    private String tableHeadBackgroundColor ="C4DBD4";
    /**
     * 表格背景颜色
     */
    private String tableCellBackgroundColor = "FFFFFF";

    /**
     * 交替行颜色
     */
    //private String[] cellBaseColors = {"FFFFFF","D9E2F3"};
    private String[] cellBaseColors = {"FFFFFF","FFFFFF"};

    /**
     * 表格文本颜色颜色
     */
    private String tableCellColor = "000000";

    /**
     * 表头文本颜色颜色
     */
    //private String tableHeadCellColor = "FFFFFF";
    private String tableHeadCellColor = "000000";
    /**
     * 表格文本字体大小
     */
    private int tableHeadCelltFontSize = 10;
    /**
     * 表格文本字体大小
     */
    private int tableCelltFontSize = 10;
    /**
     * 表格头部单元格对齐方式
     */
    private XWPFTableCell.XWPFVertAlign tableHeadAlign = XWPFTableCell.XWPFVertAlign.CENTER;

    private int tableHeadWidth = 8500;

    /**
     * 表格边框颜色
     */
    private String tableBorderColer = "8EAADB";
    /**
     * 表格行高
     */
    private int rowHeight = 360;

    /**
     * 是否开启交替行颜色
     */
    private boolean supersedeFlag = true;

    /**
     * 表头
     */
    @Getter(AccessLevel.PUBLIC)
    @Setter(AccessLevel.PRIVATE)
    private java.util.List<TableCol> tableColList;
    /**
     * 数据
     */
    @Getter(AccessLevel.PUBLIC)
    @Setter(AccessLevel.PRIVATE)
    private Collection<Object> values;

    public WordTableOpt(WordBuilder wordBuilder,
                        java.util.List<TableCol> tableColList,
                        Collection values,boolean supersedeFlag){
        this.wordBuilder = wordBuilder;

        this.values = values;
        this.tableColList = tableColList;
        this.supersedeFlag = supersedeFlag;

    }

    /**
     * 获得数据单元格的背景颜色
     *
     * @param index 当前行号0开始
     */
    private String getCellBgColor(int index){
        if(!this.supersedeFlag){
            return this.tableCellBackgroundColor;
        }
        return cellBaseColors[index % 2];
    }

    public void buildTabel(){

        // 创建表格
        XWPFTable table = this.wordBuilder.getDocument().createTable();
        table.setWidthType(TableWidthType.PCT);
        //表格宽度%
        table.setWidth(1 * this.tableHeadWidth);
        table.setTableAlignment(TableRowAlign.CENTER);
//        table.setTopBorder(XWPFTable.XWPFBorderType.THICK, 1, 0, this.tableBorderColer);
//        table.setBottomBorder(XWPFTable.XWPFBorderType.THICK, 1, 0, this.tableBorderColer);
//        table.setLeftBorder(XWPFTable.XWPFBorderType.THICK, 1, 0, this.tableBorderColer);
//        table.setRightBorder(XWPFTable.XWPFBorderType.THICK, 1, 0, this.tableBorderColer);

        CTTblBorders borders = table.getCTTbl().getTblPr().addNewTblBorders();
        CTBorder hBorder = borders.addNewInsideH();
        //边框样式 参考STBorder.class
        hBorder.setVal(STBorder.THICK);
        //边框粗细
        hBorder.setSz(BigInteger.valueOf(1));
        //边框颜色
        hBorder.setColor(this.tableBorderColer);

        CTBorder vBorder = borders.addNewInsideV();
        vBorder.setVal(STBorder.THICK);
        vBorder.setSz(BigInteger.valueOf(1));
        vBorder.setColor(this.tableBorderColer);

        CTBorder lBorder = borders.addNewLeft();
        lBorder.setVal(STBorder.THICK);
        lBorder.setSz(BigInteger.valueOf(1));
        lBorder.setColor(this.tableBorderColer);

        CTBorder rBorder = borders.addNewRight();
        rBorder.setVal(STBorder.THICK);
        rBorder.setSz(BigInteger.valueOf(1));
        rBorder.setColor(this.tableBorderColer);

        CTBorder tBorder = borders.addNewTop();
        tBorder.setVal(STBorder.THICK);
        tBorder.setSz(BigInteger.valueOf(1));
        tBorder.setColor(this.tableBorderColer);

        CTBorder bBorder = borders.addNewBottom();
        bBorder.setVal(STBorder.THICK);
        bBorder.setSz(BigInteger.valueOf(1));
        bBorder.setColor(this.tableBorderColer);


        createTableHead(table);

        // 添加表格内容
        createTableContent(table);
    }

    private void createTableHead(XWPFTable table){
        // 添加表头行
        XWPFTableRow headerRow = table.getRow(0);
        headerRow.setHeight(rowHeight);
        boolean firstHeadCell = true;

        for (TableCol tableCol : tableColList) {
            XWPFTableCell cell = null;
            if(firstHeadCell){
                cell = headerRow.getCell(0);
                firstHeadCell = false;
            }else {
                cell = headerRow.addNewTableCell();
            }

            //单元格默认有一个段落
            XWPFParagraph paragraph =  cell.getParagraphArray(0);
            paragraph.setVerticalAlignment(TextAlignment.CENTER);
            paragraph.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun run = paragraph.createRun();
            run.setText(tableCol.getHeadName());
            run.setFontSize(this.tableHeadCelltFontSize);
            run.setColor(this.tableHeadCellColor);
            //run.setFontFamily("宋体");
            run.setFontFamily("STSong-Light");
            run.setBold(true);
            //cell.setColor("4472C4");


            //cell.setWidthType(TableWidthType.AUTO);
            cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
            // 单元格背景颜色
            cell.setColor(this.tableHeadBackgroundColor);

        }

    }


    private void createTableContent(XWPFTable table){
        int index = 0;
        if(this.values != null && this.tableColList != null && this.tableColList.size() > 0){
            for (Object entry : this.values) {
                XWPFTableRow dataRow = table.createRow();
                dataRow.setHeight(rowHeight);
                int cellIndex = 0;
                for(TableCol tableCol : tableColList){
                    String text = "";
                    Object value = XReflect.getFieldValueByAttrPath(entry,tableCol.getAttrName());
                    if(value != null){
                        text = value.toString();
                    }
                    XWPFTableCell cell = dataRow.getCell(cellIndex);
                    XWPFParagraph paragraph =  cell.getParagraphArray(0);
                    paragraph.setVerticalAlignment(TextAlignment.CENTER);
                    paragraph.setAlignment(ParagraphAlignment.CENTER);
                    XWPFRun run = paragraph.createRun();
                    run.setText(text);
                    run.setFontSize(tableCelltFontSize);
                    run.setColor(this.tableCellColor);
                    //run.setFontFamily("宋体");
                    run.setFontFamily("STSong-Light");
                    //run.setColor();
                    cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
                    // 单元格背景颜色 D9E2F3
                    cell.setColor(this.getCellBgColor(index));
                    cellIndex++;
                }
                index++;
            }
        }

    }
}
